package com.sita.bike.utils;

import android.app.Activity;
import android.content.Context;
import android.widget.Toast;

import com.sita.bike.R;
import com.sita.bike.support.Constants;
import com.sita.bike.support.GlobalContext;
import com.sita.friend.ui.activity.MainActivity;
import com.sita.tboard.util.TextUtils;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.bean.SocializeEntity;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.listener.SocializeListeners;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.weixin.controller.UMWXHandler;

/**
 * Created by hongyun on 2016/3/28.
 */
public class ShareUtils {

    private static String mResrouceId;

    public static void doShareResource(String resourceid, String img_url, String title, Context context, Activity activity) {
        String url = "/cst/app/resource_share.html?resourceId=" + resourceid;
        doShare(title, url, img_url, context, activity);
        mResrouceId = resourceid;
    }

    public static void doShareRoute(long routeid, String mileage, Context context, Activity activity) {
        String url = "/cst/app/routeshare.html?routeId=" + routeid;
        String title = context.getResources().getString(R.string.track_finish_title) + mileage +
                context.getResources().getString(R.string.units_distance_km);
        doShare(title, url, null, context, activity);
    }

    private static void doShare(String title, String targetUrl, String img_url, final Context context, Activity activity) {
        final String baseUrl = Constants.BASE_URI;
        targetUrl = baseUrl + targetUrl;
        final UMSocialService mController = UMServiceFactory.getUMSocialService("com.umeng.share");
        Context mContext = (context == null) ? GlobalContext.getGlobalContext() : context;
        title = (android.text.TextUtils.isEmpty(title)) ? mContext.getResources().getString(R.string.umengshare_title) : title;

        // 设置分享内容
        mController.setShareContent(title);
        // 设置分享图片, 参数2为图片的url地址
        mController.setShareMedia(
                (img_url == null) ? new UMImage(mContext, R.mipmap.ic_launcher) : new UMImage(mContext,
                        img_url));

        mController.getConfig().removePlatform(SHARE_MEDIA.RENREN, SHARE_MEDIA.DOUBAN);

        // 添加微信平台
        UMWXHandler wxHandler = new UMWXHandler(mContext, mContext.getResources().getString(R.string.wxapp_Id),
                mContext.getResources().getString(R.string.wxapp_Secret));
        wxHandler.setTitle(title);
        wxHandler.setTargetUrl(targetUrl);
        wxHandler.addToSocialSDK();

        // 添加微信朋友圈
        UMWXHandler wxCircleHandler = new UMWXHandler(mContext, mContext.getResources().getString(R.string.wxapp_Id),
                mContext.getResources().getString(R.string.wxapp_Secret));
        wxCircleHandler.setToCircle(true);
        wxCircleHandler.setTitle(title);
        wxCircleHandler.setTargetUrl(targetUrl);
        wxCircleHandler.addToSocialSDK();

        //在显示分享面板中显示指定的平台
        mController.getConfig().setPlatforms(SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE);
        mController.openShare((activity == null) ? MainActivity.getInstance() : activity, false);

        SocializeListeners.SnsPostListener  mSnsPostListener  = new SocializeListeners.SnsPostListener() {

            @Override
            public void onStart() {

                if (!android.text.TextUtils.isEmpty(mResrouceId)) {
                    StaticsUtils.staticsShare(mResrouceId);
                    mResrouceId = "";
                }
                LogUtils.d("share", "on start");
            }

            @Override
            public void onComplete(SHARE_MEDIA platform, int stCode,
                                   SocializeEntity entity) {
                LogUtils.d("share", "code:" + stCode);

            }
        };
        mController.registerListener(mSnsPostListener);
    }
}
